<template>
  <div class="customer-signed-container">
    <top-tip :tipcontent="tipcontent" />
    <el-tabs
      v-model="queryForm.showType"
      type="border-card"
      @tab-click="handleTabs"
    >
      <el-tab-pane
        v-for="(item, index) in tabs"
        :key="index"
        :label="item.label"
        :name="item.name"
      >
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="ID"
            prop="CustomerID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="历史跟进" width="200">
            <template #default="{ row }">
              <el-button text @click="getCustomerTrack(row)">
                历史跟进
              </el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="300">
            <template #default="{ row }">
              <el-button text @click="goCustomer(row)">详情</el-button>
              <el-button text @click="handleEdit(row)">编辑</el-button>
              <el-button text @click="goOrder(row)">业务单</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getCustomerPickList } from '@/api/customer'
  import Edit from './components/CustomerSignedEdit'
  import TopTip from '@/components/TopTip'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'CustomerSigned',
    components: { Edit, TopTip },
    setup() {
      const router = useRouter()
      const state = reactive({
        tipcontent: {
          title: '签约合作中客户',
          memo:
            '下属签约合作中客户：组内签过的，合作中的客户资源；<br>' +
            '我的签约合作中客户：自己签过的，合作中的客户资源；',
        },
        tabs: [
          { label: '我签的合作中客户', name: 0 },
          { label: '下属签的合作中客户', name: 1 },
        ],
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          showType: 0,
          pageNo: 1,
          pageSize: 10,
          title: '',
          PickState: 3,
        },
      })

      const businessTrack = (row) => {
        router.push({
          path: '/customer/track',
          query: { customerid: row.CustomerID },
        })
      }
      const getCustomerTrack = (row) => {
        router.push({
          path: '/customer/trackingBusiness',
          query: { customerid: row.CustomerID },
        })
      }
      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const goCustomer = (row) => {
        router.push({
          path: '/customer/info',
          query: { customerid: row.CustomerID },
        })
      }

      const goOrder = (row) => {
        router.push({
          path: '/business/order/orderList',
          query: { customerid: row.CustomerID },
        })
      }
      const handleTabs = (tab) => {
        console.log('handleTabs', tab.index, tab.props.name)
        state.queryForm.showType = tab.props.name
        fetchData()
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getCustomerPickList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        handleTabs,
        businessTrack,
        setSelectRows,
        getCustomerTrack,
        handleEdit,
        goCustomer,
        goOrder,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
